package com.hndfsj.common.web;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.botaruibo.xvcode.generator.Generator;
import com.hndfsj.admin.service.IDictionaryService;
import com.hndfsj.admin.service.IUserService;
import com.hndfsj.common.domain.TokenModel;
import com.hndfsj.common.service.ITokenService;
import com.hndfsj.common.service.IValuesService;
import com.hndfsj.framework.common.MReturnObject;
import com.hndfsj.framework.config.RWAdminConstant;
import com.hndfsj.framework.utils.ValidateCodeGenerator;

/**
 * 共用方法Controller的提取
 *
 * @author zxj
 * @date 2017-3-10 上午09:17:04
 */

@Controller
@RequestMapping(value = "/common")
public class CommonController {
	static Logger log = LoggerFactory.getLogger(CommonController.class);
	@Resource
	IDictionaryService dictionaryService;
	@Resource
	IValuesService valuesService;
	@Resource
	IUserService userService;
	@Resource
	private ITokenService tokenService;

	@RequestMapping("/token/check")
	public @ResponseBody MReturnObject tokenCheck(@RequestParam String token,HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		TokenModel tokenModel=tokenService.getToken(token);
		if(tokenModel==null) {
			return new MReturnObject(MReturnObject.ERROR,"token invalid");
		}
		return new MReturnObject(MReturnObject.SUCCESS, tokenModel);
	}
	@RequestMapping("/validateCode")
	public void validateCode(HttpServletRequest request, HttpServletResponse response, Model model) throws IOException {
		try {
			Generator generator = ValidateCodeGenerator.generatorPng();
			generator.write2out(response.getOutputStream()).close();
			String validCode = generator.text(); // 验证码
			response.setContentType("image/png");
			request.getSession().setAttribute(RWAdminConstant.SESSION_VALIDATECODE, validCode);
		} catch (Exception e) {
			response.getWriter().write("echostr");
		}

	}
}
